//
//  LOadImageView.m
//  wulihan
//
//  Created by tang on 16/3/28.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LOadImageView.h"
#import "Header.h"

//#import "IQStickerView.h"

@interface LOadImageView ()<UIScrollViewDelegate>
{
    UIImageView *backIMageVIew;
    UIScrollView *mainScrollview;
    float scaleFloat;
    

    UIImage *sourceImage;
    
    CGPoint point;
    UIView *backView;
    
    
    
    float contentOffsetX;
    float contentOffentY;
    CGFloat zoomFloat;
    
}

@end


@implementation LOadImageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        scaleFloat = 0.0;
        
        [self setUserInteractionEnabled:YES];
        [self initUI];
        
        
//        [self setBackgroundColor:[UIColor redColor]];
    }
    return self;
}


- (void)initUI
{
    [self setBackgroundColor:uicolor(62, 62, 62)];
     backIMageVIew = [[UIImageView alloc] initWithFrame:self.bounds];
    [backIMageVIew setUserInteractionEnabled:YES];
    [self addSubview:backIMageVIew];
    

}



#pragma mark----设置气泡

//加载图片
- (UIImage *)loadImageWithimage:(UIImage *)image
{
    [backIMageVIew setImage:image];
    return [self cropImage];
}


//裁剪图片
- (UIImage *)cropImage
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(WithView, WithView), NO, 0.0);
    
//    NSLog(@"++++++%f",self.frame.size.width);
    
    [self drawViewHierarchyInRect:CGRectMake(0, 0, WithView, WithView) afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}




- (void)scaleTView:(UIPinchGestureRecognizer *)recognizer
{
    if(recognizer.state== UIGestureRecognizerStateBegan) {
        scaleFloat = 1.0;
    }
    
    CGFloat scale = 1.0 - (scaleFloat - [recognizer scale]);
    
    CGAffineTransform currentTransform = backIMageVIew.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    [backIMageVIew setTransform:newTransform];
    
    
    scaleFloat = [recognizer scale];
//    NSLog(@"%f", scaleFloat);
}


//移动
- (void)panToRemove:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];
    
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
        
        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
        
        point = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
        backView = [piece superview];
    }
    else if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
    {
        [_MLOadImageViewDelegate saveImageWith:point.x withY:point.y withGes:gestureRecognizer];
    }
    
}




- (void)setViewWithPanX:(float)X withY:(float)Y WithGes:(UIPanGestureRecognizer *)gestureRecognizer
{
    
    if (X == 10000)
    {
        
    }
    else
    {
        UIView *piece = [gestureRecognizer view];
        //    CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
        //
        [piece setCenter:CGPointMake(X, Y)];
        //
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];

    }
    
}



//旋转
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
    // Convert the radian value to show the degree of rotation
//    NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
    [backIMageVIew setTransform:CGAffineTransformMakeRotation([recognizer rotation])];
}




- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//    NSLog(@"%f", backIMageVIew.bounds.size.height);
    return backIMageVIew;
}


- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//    NSLog(@"%f", backView.bounds.size.width);
    CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
    
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;
    
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;
    
    backIMageVIew.center = CGPointMake(xcenter, ycenter);
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    
    contentOffsetX = scrollView.contentOffset.x;
    contentOffentY = scrollView.contentOffset.y;
    zoomFloat = scrollView.zoomScale;

    if (_MLOadImageViewDelegate && [_MLOadImageViewDelegate respondsToSelector:@selector(saveImageWithScale:withContentWithX:withY:)])
    {
        [_MLOadImageViewDelegate saveImageWithScale:zoomFloat withContentWithX:contentOffsetX withY:contentOffentY];
    }
    
    
}



//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    NSLog(@"%f", scrollView.contentOffset.x);
//}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    contentOffsetX = scrollView.contentOffset.x;
    contentOffentY = scrollView.contentOffset.y;
    zoomFloat = scrollView.zoomScale;
    
    
    if (_MLOadImageViewDelegate && [_MLOadImageViewDelegate respondsToSelector:@selector(saveImageWithScale:withContentWithX:withY:)])
    {
        [_MLOadImageViewDelegate saveImageWithScale:zoomFloat withContentWithX:contentOffsetX withY:contentOffentY];
    }

}














//- (void)setCropRect:(CGRect)cropRect
//{
////    NSLog(@"%f", cropRect.origin.x);
////    NSLog(@"%f", cropRect.origin.y);;
//    
//    if(!CGRectEqualToRect(_cropRect,cropRect)){
//        
//        _cropRect = CGRectOffset(cropRect, self.frame.origin.x, self.frame.origin.y);
//        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
//        CGContextRef context = UIGraphicsGetCurrentContext();
//        [[UIColor yellowColor] setFill];
//        UIRectFill(self.bounds);
//        CGContextSetStrokeColorWithColor(context, [[UIColor redColor] colorWithAlphaComponent:0.0].CGColor);
//        CGContextStrokeRect(context, cropRect);
//        [[UIColor clearColor] setFill];
//        UIRectFill(CGRectInset(cropRect, 0, 0));
//        boderImageView.image = UIGraphicsGetImageFromCurrentImageContext();
//        
//        UIGraphicsEndImageContext();
//    }
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
